<template>
  <div class="question-index">
    <el-row class="button-group" :gutter="44">
      <el-col :span="6">
        <el-card class="box-card" @click.native="goAnswer">
          <img src="../../assets/icon3.png" alt="">
          <p style="color: #FFA200;">随机练习</p>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card" @click.native="showDialog(1)">
          <img src="../../assets/icon4.png" alt="">
          <p style="color: #6DC942;">指定区域练习</p>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card" @click.native="showDialog(2)">
          <img src="../../assets/icon1.png" alt="">
          <p style="color: #F47D94;">指定部位练习</p>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card" @click.native="showDialog(3)">
          <img src="../../assets/icon2.png" alt="">
          <p style="color: #60C1FD;">指定用途练习</p>
        </el-card>
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="序号"
        width="200"
        align="center"
      />
      <el-table-column
        prop="author"
        label="用户"
      />
      <el-table-column
        prop="date"
        label="时间"
      />
      <el-table-column
        label="正确率"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.accuracy }}%
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-link size="mini" type="primary" @click="goDetail(scope.row)">查看</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-size="10"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleSizeChange"
    />

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-select v-model="value" size="mini" clearable placeholder="请选择">
        <el-option
          v-for="item in option"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { query, options } from '@/api/question'
import { parseTime } from '@/utils'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      dialogVisible: false,
      value: null,
      option: null,
      title: null,
      index: 1,
      options: [{
        title: '选择区域',
        list: []
      }, {
        title: '选择部位',
        list: []
      }, {
        title: '选择用途',
        list: []
      }]
    }
  },
  mounted() {
    this.getOptions()
    this.getList()
  },
  methods: {
    async getList() {
      const params = {
        page_num: this.currentPage,
        order_info: { key: 'id', order: 'asc' },
        page_size: 10
      }
      const res = await query(params)
      if (res.code == 200 && res.data) {
        this.tableData = res.data
        this.total = res.paging.total
        this.tableData.map(item => {
          item.accuracy = item.accuracy ? (item.accuracy * 100) : 0
          item.date = parseTime(item.create_time)
          return item
        })
      }
    },
    async getOptions() {
      const res = await options()
      if (res.code == 200 && res.data) {
        this.options[0].list = res.data.war_zone
        this.options[1].list = res.data.plant_parts
        this.options[2].list = res.data.use
      }
    },
    goAnswer() {
      this.$router.push('/question/answer/0')
    },
    showDialog(index) {
      if (index == 1) {
        this.title = this.options[index - 1].title
        this.option = this.options[index - 1].list
      } else if (index == 2) {
        this.title = this.options[index - 1].title
        this.option = this.options[index - 1].list
      } else if (index == 3) {
        this.title = this.options[index - 1].title
        this.option = this.options[index - 1].list
      }
      this.index = index
      this.dialogVisible = true
    },
    confirm() {
      this.$router.push(`/question/answer/${this.index}&${this.value}`)
    },
    handleClose(done) {
      done()
    },
    goDetail({ id }) {
      this.$router.push(`/question/detail/${id}`)
    },
    handleSizeChange(val) {
      this.currentPage = val
      this.getList()
    }
  }
}
</script>

<style lang="scss">
.question-index{
  position: relative;
  min-height: calc(100vh - 150px);
  padding-bottom: 100px;
  background: #FFFFFF;
  .el-pagination{
    margin-top: 20px;
  }
  .button-group{
    margin-left: 0 !important;
    margin-right: 0 !important;
    margin-bottom: 34px;
    .el-col:nth-child(1){
      padding-left: 0 !important;
    }
    .el-col:nth-child(4){
      padding-right: 0 !important;
    }
    .box-card{
      width: 100%;
      height: 146px;
      background: #FFFFFF;
      border: 1px solid #DCDCDC;
      border-radius: 10px;
      cursor: pointer;
      .el-card__body{
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 0;
        width: 100%;
        height: 100%;
        img{
          width: 76px;
          height: 76px;
          transition: all ease-in .3s;
        }
        p{
          margin: 0;
          margin-left: 15px;
          font-size: 24px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          color: #6DC942;
          line-height: 44px;
          transition: all ease-in .3s;
        }
      }
    }
    .box-card:hover{
      img{
        width: 82px;
        height: 82px;
      }
      p{
        font-size: 25px;
      }
    }
  }
  .el-pagination{
    position: absolute;
    bottom: 0;
    left: 50%;
    margin-left: - 300px;
    width: 600px;
  }
}
</style>
